package cn.e2win.biz.ds;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.e2win.biz.comm.ViewExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysLabel;
import cn.e2win.base.entity.SysUser;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.base.json.vo.Result;
import cn.e2win.biz.base.BaseController;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.base.CommonService;
import cn.e2win.biz.dbo.DsEmp;
import cn.e2win.biz.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;


@Controller
@Scope("request")
@RequestMapping("/ds")
public class DsController extends BaseController {

	@Autowired
	private DsService dsService;
	@Autowired
	private CommonService commonService;
	
	@Override
	public BaseService initService() {
		return dsService;
	}

	@RequestMapping("selfInfoDsEmp")
	public String selfInfoDsEmp(HttpServletRequest request, ModelMap modelMap){
		Integer id = pager.getCurrUser().getEmpId();
		String idStr = request.getParameter("id");
		if(!StringUtils.isEmpty(idStr)){
			id = Integer.parseInt(idStr);
		}
		
		toDsEditor(request, id, modelMap);
		return "ds/selfInfoDsEmp";
	}


	@RequestMapping("toDsEditor/{id}")
	public String toDsEditor(HttpServletRequest request, @PathVariable(ID) Integer id, ModelMap modelMap){
		DsEmp empty = new DsEmp(id);
		String clazz = DsEmp.class.getSimpleName();
		Map<String, Object> obj = dsService.getEntity(empty);
		modelMap.put(OBJECT, obj);
		List<SysLabel> labelIndustryList = commonService.getSysLabelList(empty, "labelIndustry");
		List<SysLabel> labelFocusOnList = commonService.getSysLabelList(empty,"labelFocusOn");	
		List<SysLabel> labelHelpForList = commonService.getSysLabelList(empty,"labelHelpFor");						
		modelMap.put("labelIndustryList", labelIndustryList);
		modelMap.put("labelFocusOnList", labelFocusOnList);
		modelMap.put("labelHelpForList", labelHelpForList);

		List<SysLabel> allLabelIndustryList = commonService.getSysLabelList(clazz,"labelIndustry");
		List<SysLabel> allLabelFocusOnList = commonService.getSysLabelList(clazz,"labelFocusOn");
		List<SysLabel> allLabelHelpForList = commonService.getSysLabelList(clazz,"labelHelpFor");
		modelMap.put("allLabelIndustryList", allLabelIndustryList);
		modelMap.put("allLabelFocusOnList", allLabelFocusOnList);
		modelMap.put("allLabelHelpForList", allLabelHelpForList);
		return "ds/dsEditor";
	}

	@RequestMapping("editDs")
	@ResponseBody
	public Result editDs(HttpServletRequest request,@ModelAttribute DsEmp obj,ModelMap modelMap) {
		Result r = new Result();
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		try{
			if(obj.getId()!=null) {
				DsEmp empty = new DsEmp(obj.getId());
				List<SysLabel> sysLabelList = new ArrayList<SysLabel>();
				sysLabelList.addAll(commonService.getSysLabelList(empty, "labelIndustry"));
				sysLabelList.addAll(commonService.getSysLabelList(empty, "labelFocusOn"));
				sysLabelList.addAll(commonService.getSysLabelList(empty, "labelHelpFor"));
				commonService.delLabel(sysLabelList);
			}
			dsService.editDs(obj);
		} catch (MyException e) {
			logger.error(e);
			r.setStatusCode(Result.FAILED);
			r.setException(e);
		}
		return r;
	}

	@RequestMapping("editSelfInfoDsEmp")
	@ResponseBody
	public Result editSelfInfoDsEmp(HttpServletRequest request,@ModelAttribute DsEmp obj,ModelMap modelMap){
		Result r = new Result();
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		
		try{
			DsEmp ds = (DsEmp)dsService.getEntity(DsEmp.class, obj.getId());
			String mdfStatus = obj.getMdfStatus();
			//导师自助修改
			if("SUBMITED".equals(mdfStatus)){
				ds.setDwNameNew(obj.getDwNameNew());
				ds.setZwNew(obj.getZwNew());
				ds.setHonorNew(obj.getHonorNew());
				ds.setMdfStatus(mdfStatus);
				
				ds.setLabelFocusOn(obj.getLabelFocusOn());
				ds.setLabelHelpFor(obj.getLabelHelpFor());
				ds.setLabelIndustry(obj.getLabelIndustry());
				ds.setIcon(obj.getIcon());
			//自助修改审核
			}else if("APPROVED".equals(mdfStatus)){
	
				if(!StringUtils.isEmpty(ds.getDwNameNew())){
					ds.setDwName(ds.getDwNameNew());
				}
				if(!StringUtils.isEmpty(ds.getZwNew())){
					ds.setZw(ds.getZwNew());
				}
				if(!StringUtils.isEmpty(ds.getHonorNew())){
					ds.setHonor(ds.getHonorNew());
				}
				
				ds.setDwNameNew("");
				ds.setDwNameNew("");
				ds.setZwNew("");
				ds.setHonorNew("");
				
				ds.setMdfStatus("");
			//自助修改驳回
			}else if("REJECTED".equals(mdfStatus)){
				ds.setMdfStatus(mdfStatus);
			}
			
			dsService.editDs(ds);

		} catch (MyException e) {
			logger.error(e);
			r.setStatusCode(Result.FAILED);
			r.setException(e);
		}
		return r;
	}

	@RequestMapping("dsPager")
	public String dsPager(HttpServletRequest request, ModelMap modelMap){

		if(StringUtils.isEmpty(pager.getOrderField())){
			pager.setOrderField("orderInt");
			pager.setOrderDirection("");
			
		}
		
		if("APPROVE".equals(todo)){//资格审核
			pager.getScMap().put("approveStatus", "SUBMITED");
		}else if("EDIT".equals(todo)){//修改提交
			pager.getScMap().put("approveStatus_IN", "UNSUBMIT【】REJECTED");
		}else if("UPDATE".equals(todo)){//指导中心直接修改
			//pager.getScMap().put("approveStatus", "APPROVED");
		}else if("MDF_APPROVE".equals(todo)){//更新审核
			pager.getScMap().put("mdfStatus", "SUBMITED");
		}
		
		setScMap(pager);
		
		dsService.pagerDs(pager);
		modelMap.put("pager", pager);
		return "ds/dsPager";
	}

	@RequestMapping("dsPagerExcel")
	public ModelAndView customerPagerExcel(HttpServletRequest request, ModelMap modelMap) {
		pager.setReturnAll(true);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("key",dsPager(request,modelMap));
		model.put("list", pager.getResultList());
		return new ModelAndView(new ViewExcel(), model);
	}

	private void setScMap(Pager pager){

		SysUser user = pager.getCurrUser();
		String user_I = pager.getScMap().get("user_I");
		
		//我的工作区
		if(!StringUtils.isEmpty(user_I)){
			//外部用户，导师及创业之家用户
			if(user.getUserType().equals("DsEmp") || user.getUserType().equals("CyzjEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
					
				//普通工作人员
				}else{
				}
			//内部用户，即指导中心用户	
			}else if(user.getUserType().equals("ZdzxEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
					pager.getScMap().remove("user_I");
				//普通工作人员
				}else{
					
				}
			}
		//非我的工作区
		}else{
			//外部用户，导师及创业之家用户
			if(user.getUserType().equals("DsEmp") || user.getUserType().equals("CyzjEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
					
				//普通工作人员
				}else{
					
				}
				pager.getScMap().put("approveStatus", "APPROVED");
			//内部用户，即指导中心用户	
			}else if(user.getUserType().equals("ZdzxEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
				//普通工作人员
				}else{
					//资格审核，资料更新审核， 资料修改
					if("APPROVE".equals(todo) || "MDF_APPROVE".equals(todo)  || "UPDATE".equals(todo)){
						pager.getScMap().put("adminUserId", user.getId()+"");
					//修改
					}else if("EDIT".equals(todo)){
						pager.getScMap().put("createById", user.getId()+"");
					}
					
				}
			}
		}

		
	}


}
